package de.haw.halloWorld;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{

	public final static String EXTRA_MESSAGE = "de.awallis.halloWorld.MESSAGE";
	public static Context contextOfApplication; 
	Prefs prefs = new Prefs();
	
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        
        //build the GUI
        setContentView(R.layout.activity_main);
        
        contextOfApplication = getApplicationContext(); 
                
        //get the current version of the application 
        int version = getVersion(); 
       	
        //save the version in the SharedPreferences
        //prefs.saveVersion(version);
        
        //get current Version and check for a newer one 
        int savedVersion = prefs.loadVersion(); 
        
        if(version > savedVersion){
        	Toast toast = Toast.makeText(contextOfApplication, "new Version available", Toast.LENGTH_LONG);
        	toast.show();
        }
        
        /*
        Toast toast = Toast.makeText(getApplicationContext(), "saved version: " + Integer.toString(savedVersion), Toast.LENGTH_LONG);
        toast.show();
        */
        
        //initialize new Button and add a onClickMethod 
        final Button button = (Button) findViewById(R.id.button_send);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast toast = Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT);
				toast.show();
				
				Intent intent = new Intent(getApplicationContext(), MessageDisplayActivity.class);
				EditText editText = (EditText) findViewById(R.id.edit_message);
				String message = editText.getText().toString();
				intent.putExtra(EXTRA_MESSAGE, message);
				startActivity(intent);
			}
		});  
        
        BasicListFragment messageList = new BasicListFragment();
 
        getFragmentManager().beginTransaction().add(R.id.fragment_container, messageList).commit();
	}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Toast toast; 
    	//handle the item selection 
    	switch(item.getItemId()){
    		case R.id.unregister:
    			toast = Toast.makeText(contextOfApplication, "Unregister", Toast.LENGTH_LONG);
    			toast.show();
    			return true;
    		case R.id.status:
    			toast = Toast.makeText(contextOfApplication, "Status", Toast.LENGTH_LONG);
    			toast.show();
    			return true;
    		default:
    	    	return super.onOptionsItemSelected(item);
    	}
    }
    
    // get the current version of the application
    public int getVersion(){
    	int version = -1; 
    
		try {
			PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
	    	version = pinfo.versionCode;
		} catch (NameNotFoundException e) {
			Log.e(this.getClass().getSimpleName(), "Name not found", e);
		}

    	return version; 
    }
    
    //Getter and Setter
    public static Context getContextOfApllication(){
    	return contextOfApplication;
    }
}
